package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Dremeue
 * @create 2019-10-26 1:00
 **/
@Controller
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("list")
    public ResponseEntity<List<Category>> queryCategoriesByPid(@RequestParam(value = "pid", defaultValue = "0") Long
                                                                       pid) {
        if (pid == null || pid < 0) {

            // 响应400
            return ResponseEntity.badRequest().build();
        }

        List<Category> categories = categoryService.queryCategoriesByPid(pid);

        if (CollectionUtils.isEmpty(categories)) {
            // 响应400
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(categories);
    }
}
